package com.weixin.servlet.backstage;

import java.io.IOException;

import java.io.UnsupportedEncodingException;
import java.nio.file.Path;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.jspsmart.upload.Files;
import com.jspsmart.upload.SmartUpload;
import com.weixin.service.IBackstageService;
import com.weixin.service.impl.BackstageServiceImpl;
import com.weixin.vo.Goods;
import com.weixin.vo.GoodsType;

/**
 * 商品处理servlet
 * @author 唐鑫明
 *
 */
@WebServlet("/GoodsServlet/*")
public class GoodsServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	IBackstageService backstageService = new BackstageServiceImpl();
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String pathInfo = request.getPathInfo().substring(1);
		if("listGoodsInfo".equals(pathInfo)) {
			listGoodsInfo(request,response);
		}else if("editGoodsState".equals(pathInfo)) {
			editGoodsState(request,response);
		}else if("selectGoodsSales".equals(pathInfo)) {
			selectGoodsSales(request,response);
		}else if("gtypeList".equals(pathInfo)) {
			gtypeList(request,response);
		}else if("goodsInfoById".equals(pathInfo)) {
			goodsInfoById(request,response);
		}else if("deleteGoodsById".equals(pathInfo)) {
			deleteGoodsById(request,response);
		}else if("deleteSelectedGoods".equals(pathInfo)) {
			deleteSelectedGoods(request,response);
		}else if("preEditGoods".equals(pathInfo)) {
			preEditGoods(request,response);
		}else if("editGoods".equals(pathInfo)) {
			editGoods(request,response);
		}else if("picture".equals(pathInfo)) {
			editPicture(request,response);
		}else if("preAddGoods".equals(pathInfo)) {
			preAddGoods(request,response);
		}else if("addPicture".equals(pathInfo)) {
			addPicture(request,response);
		}else if("addGoods".equals(pathInfo)) {
			addGoods(request, response);
		}else if("checkGoods".equals(pathInfo)) {
			checkGoods(request,response);
		}
	}
	/**
	 * 查看是否存在该商品
	 * @param request
	 * @param response
	 */
	private void checkGoods(HttpServletRequest request, HttpServletResponse response) {
			int goods_id = 0;
			if(request.getParameter("goods_id")!=null&&!(request.getParameter("goods_id").equals(""))) {
				goods_id = Integer.valueOf(request.getParameter("goods_id"));
			}
			try {
				//调用业务层的方法查找是否存在该商品
				Goods goods = backstageService.checkGoods(goods_id);
				if(goods!=null) {
					response.getWriter().println(1);
				}else {
					response.getWriter().println(0);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
	}

	/**
	 * 添加商品方法
	 * @param request
	 * @param response
	 */
	private void addGoods(HttpServletRequest request, HttpServletResponse response) {
		//设置编码
		try {
			request.setCharacterEncoding("utf-8");
		} catch (Exception e) {
			e.printStackTrace();
		}
		int goods_id = 0;
		if(request.getParameter("goods_id")!=null&&!("".equals(request.getParameter("goods_id")))) {
			goods_id = Integer.valueOf(request.getParameter("goods_id"));
		}
		String goods_name = null;
		if(request.getParameter("goods_name")!=null&&!("".equals(request.getParameter("goods_name")))) {
			goods_name = request.getParameter("goods_name");
		}
		double goods_price = 0;
		if(request.getParameter("goods_price")!=null&&!("".equals(request.getParameter("goods_price")))) {
			goods_price = Double.valueOf(request.getParameter("goods_price"));
		}
		int goods_count = 0;
		if(request.getParameter("goods_count")!=null&&!("".equals(request.getParameter("goods_count")))) {
			goods_count = Integer.valueOf(request.getParameter("goods_count"));
		}
		int gtype_id = 0;
		if(request.getParameter("gtype_id")!=null&&!("".equals(request.getParameter("gtype_id")))) {
			gtype_id = Integer.valueOf(request.getParameter("gtype_id"));
		}
		String goods_desc = null;
		if(request.getParameter("goods_desc")!=null&&!("".equals(request.getParameter("goods_desc")))) {
			goods_desc = request.getParameter("goods_desc");
		}
		String goods_img = null;
		if(request.getParameter("goods_img")!=null&&!("".equals(request.getParameter("goods_img")))) {
			goods_img = request.getParameter("goods_img");
		}
		Goods goods = new Goods();
		goods.setGoods_count(goods_count);
		goods.setGoods_desc(goods_desc);
		goods.setGoods_img(goods_img);
		goods.setGoods_name(goods_name);
		goods.setGoods_price(goods_price);
		goods.setGtype_id(gtype_id);
		goods.setGoods_id(goods_id);
		goods.setState_id(4);
		try {
			//调用业务层的方法添加商品
			boolean b = backstageService.addGoods(goods);
			if(b) {
				request.getRequestDispatcher("/GoodsServlet/listGoodsInfo").forward(request, response);
			}else {
				request.getRequestDispatcher("/GoodsServlet/listGoodsInfo").forward(request, response);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

	/**
	 * 添加商品图片
	 * @param request
	 * @param response
	 */
	private void addPicture(HttpServletRequest request, HttpServletResponse response) {
		 try {
			SmartUpload smart = new SmartUpload();
			smart.initialize(getServletConfig(), request, response);
			smart.setCharset("utf-8");
			//上传：
			smart.upload();
			Files files = smart.getFiles();
			String ext = files.getFile(0).getFileExt();
			String upload_fileName = UUID.randomUUID().toString()+"."+ext;
			String path = "/img/goodsImg/"+upload_fileName;
			files.getFile(0).saveAs(path);
			response.getWriter().print(path);
			}catch(Exception e){
				e.printStackTrace();
			}
	
	}

	/**
	 * 预添加商品信息
	 * @param request
	 * @param response
	 */
	private void preAddGoods(HttpServletRequest request, HttpServletResponse response) {
		//设置编码
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		try {
			//调用业务层的方法查询商品类型
			List<GoodsType> goodsTypeList = backstageService.selectGoodsType();
			//将数据传递到goods_add.jsp页面
			response.getWriter().println(new Gson().toJson(goodsTypeList));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 将商品图片上传到服务器
	 * @param request
	 * @param response
	 */
	private void editPicture(HttpServletRequest request, HttpServletResponse response) {
		 try {
			 
				SmartUpload smart = new SmartUpload();
				smart.initialize(getServletConfig(), request, response);
				smart.setCharset("utf-8");
				//上传：
				smart.upload();
				Files files = smart.getFiles();
				String ext = files.getFile(0).getFileExt();
				String upload_fileName = UUID.randomUUID().toString()+"."+ext;
				String src = "/img/goodsImg/"+upload_fileName;
				files.getFile(0).saveAs(src);
				response.getWriter().print(src);
				}catch(Exception e){
					e.printStackTrace();
				}
	}

	/**
	 * 修改商品方法
	 * @param request
	 * @param response
	 */
	private void editGoods(HttpServletRequest request, HttpServletResponse response) {
		//设置编码
		try {
			request.setCharacterEncoding("utf-8");
		} catch (Exception e) {
			e.printStackTrace();
		}
		int goods_id = 0;
		if(request.getParameter("goods_id")!=null&&!("".equals(request.getParameter("goods_id")))) {
			goods_id = Integer.valueOf(request.getParameter("goods_id"));
		}
		String goods_name = null;
		if(request.getParameter("goods_name")!=null&&!("".equals(request.getParameter("goods_name")))) {
			goods_name = request.getParameter("goods_name");
		}
		double goods_price = 0;
		if(request.getParameter("goods_price")!=null&&!("".equals(request.getParameter("goods_price")))) {
			goods_price = Double.valueOf(request.getParameter("goods_price"));
		}
		int goods_count = 0;
		if(request.getParameter("goods_count")!=null&&!("".equals(request.getParameter("goods_count")))) {
			goods_count = Integer.valueOf(request.getParameter("goods_count"));
		}
		int gtype_id = 0;
		if(request.getParameter("gtype_id")!=null&&!("".equals(request.getParameter("gtype_id")))) {
			gtype_id = Integer.valueOf(request.getParameter("gtype_id"));
		}
		String goods_desc = null;
		if(request.getParameter("goods_desc")!=null&&!("".equals(request.getParameter("goods_desc")))) {
			goods_desc = request.getParameter("goods_desc");
		}
		String goods_img = null;
		if(request.getParameter("picture")!=null&&!("".equals(request.getParameter("picture")))) {
			goods_img = request.getParameter("picture");
		}
		String picsrc = null;
		if(request.getParameter("picsrc")!=null&&!("".equals(request.getParameter("picsrc")))) {
			picsrc = request.getParameter("picsrc");
		}
		Goods goods = new Goods();
		goods.setGoods_count(goods_count);
		goods.setGoods_desc(goods_desc);
		goods.setGoods_name(goods_name);
		goods.setGoods_price(goods_price);
		goods.setGtype_id(gtype_id);
		goods.setGoods_id(goods_id);
		goods.setGoods_img(goods_img);
		if(goods_img!=null) {
			goods.setGoods_img(goods_img);
		}else {
			goods.setGoods_img(picsrc);
		}
		System.out.println("goods_img=="+goods_img);
		System.out.println("picsrc=="+picsrc);
		
		try {
			//调用业务层的方法修改商品信息
			boolean b = backstageService.editGoodsInfo(goods);
			if(b) {
				request.getRequestDispatcher("/GoodsServlet/listGoodsInfo").forward(request, response);
			}else {
				request.getRequestDispatcher("/GoodsServlet/listGoodsInfo").forward(request, response);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 预修改商品方法
	 * @param request
	 * @param response
	 */
	private void preEditGoods(HttpServletRequest request, HttpServletResponse response) {
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=uft-8");
		int goods_id = 0;
		if(request.getParameter("goods_id")!=null&&!("".equals(request.getParameter("goods_id")))) {
			goods_id = Integer.valueOf(request.getParameter("goods_id"));
		}
			try {
				//调用业务层的方法根据商品编号获取对应商品信息
				Goods goods = backstageService.selectGoodsById(goods_id);
				//调用业务层的方法获取商品类型
				List<GoodsType> goodsTypeList = backstageService.selectGoodsType();
				//将数据传到goods_edit.jsp页面
				response.getWriter().print("["+new Gson().toJson(goods)+","+new Gson().toJson(goodsTypeList)+"]");
			} catch (Exception e) {
				e.printStackTrace();
			}
	}

	/**
	 * 删除选中的商品
	 * @param request
	 * @param response
	 */
	private void deleteSelectedGoods(HttpServletRequest request, HttpServletResponse response) {
		String[] goods_ids = request.getParameterValues("goods_id");
		try {
			//调用业务层的方法删除选中的商品信息
			boolean b = backstageService.deleteSelecedGoods(goods_ids);
			if(b) {
				response.getWriter().println(1);
			}else {
				response.getWriter().println(0);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 删除单个商品
	 * @param request
	 * @param response
	 */
	private void deleteGoodsById(HttpServletRequest request, HttpServletResponse response) {
		int goods_id = 0;
		if(request.getParameter("goods_id")!=null&&!("".equals(request.getParameter("goods_id")))) {
			goods_id = Integer.valueOf(request.getParameter("goods_id"));
		}
		try {
			//调用业务层的方法删除对应的商品信息
			boolean b = backstageService.deleteGoodsById(goods_id);
			if(b) {
				response.getWriter().println(1);
			}else {
				response.getWriter().println(0);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
			
	}

	/**
	 * 根据商品编号查看对应的商品信息
	 * @param request
	 * @param response
	 */
	private void goodsInfoById(HttpServletRequest request, HttpServletResponse response) {
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		int goods_id = 0;
		if(request.getParameter("goods_id")!=null&&!("".equals(request.getParameter("goods_id")))) {
			goods_id = Integer.valueOf(request.getParameter("goods_id"));
		}
		try {
			//调用业务层的方法获取对应的商品信息
			Goods goodsById = backstageService.selectGoodsById(goods_id);
			//将数据转发到goods_look.jsp页面
			response.getWriter().println(new Gson().toJson(goodsById));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 在ecahrs中下拉列表数据
	 * @param request
	 * @param response
	 */
	private void gtypeList(HttpServletRequest request, HttpServletResponse response) {
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		try {
			//调用业务层的方法查询所有的商品类型
			List<GoodsType> goodsTypeList = backstageService.selectGoodsType();
			//将数据传递到goods_sales.jsp页面
			response.getWriter().println(new Gson().toJson(goodsTypeList));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 查询商品销量
	 * @param request
	 * @param response
	 */
	private void selectGoodsSales(HttpServletRequest request, HttpServletResponse response) {
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		int gtype_id = 1;
		if(request.getParameter("gtype_id")!=null&&!("".equals(request.getParameter("gtype_id")))) {
			gtype_id = Integer.valueOf(request.getParameter("gtype_id"));
		}
		//准备集合存放信息
		//存放商品名的集合
		List<Object> name =new ArrayList<>();
		//商品一月销量集合
		List<Object> one = new ArrayList<>();
		//商品二月销量集合
		List<Object> two = new ArrayList<>();
		//商品三月销量集合
		List<Object> three = new ArrayList<>();
		//商品四月销量集合
		List<Object> four = new ArrayList<>();
		//商品五月销量集合
		List<Object> five = new ArrayList<>();
		//商品六月销量集合
		List<Object> six = new ArrayList<>();
		//商品七月销量集合
		List<Object> seven = new ArrayList<>();
		//商品八月销量集合
		List<Object> eight = new ArrayList<>();
		//商品九月销量集合
		List<Object> nine = new ArrayList<>();
		//商品十月销量集合
		List<Object> ten = new ArrayList<>();
		//商品十一月销量集合
		List<Object> eleven = new ArrayList<>();
		//商品十二月销量集合
		List<Object> twelve = new ArrayList<>();
		//准备Map
		Map<String,Object> oneMap = new HashMap<>();
		Map<String,Object> twoMap = new HashMap<>();
		Map<String,Object> threeMap = new HashMap<>();
		Map<String,Object> fourMap = new HashMap<>();
		Map<String,Object> fiveMap = new HashMap<>();
		Map<String,Object> sixMap = new HashMap<>();
		Map<String,Object> sevenMap = new HashMap<>();
		Map<String,Object> eightMap = new HashMap<>();
		Map<String,Object> nineMap = new HashMap<>();
		Map<String,Object> tenMap = new HashMap<>();
		Map<String,Object> elevenMap = new HashMap<>();
		Map<String,Object> twelveMap = new HashMap<>();
		
		try {
			//调用业务层的方法获取各商品每月销量
			List<Map<String,Object>> goodsSales = backstageService.selectGoodsSales(gtype_id);
			Iterator<Map<String, Object>> iterator = goodsSales.iterator();
			while(iterator.hasNext()) {
				Map<String, Object> map = iterator.next();
				name.add(map.get("goods_name"));
				one.add(map.get("one"));
				two.add(map.get("two"));
				three.add(map.get("three"));
				four.add(map.get("four"));
				five.add(map.get("five"));
				six.add(map.get("six"));
				seven.add(map.get("seven"));
				eight.add(map.get("eight"));
				nine.add(map.get("nine"));
				ten.add(map.get("ten"));
				eleven.add(map.get("eleven"));
				twelve.add(map.get("twelve"));
			}
			for(int i = 0;i<name.size();i++) {
				if(i==0) {
					oneMap.put("name", name.get(0));
					oneMap.put("type", "bar");
					oneMap.put("data",new ArrayList<>());
				}else if(i==1) {
					twoMap.put("name", name.get(1));
					twoMap.put("type", "bar");
					twoMap.put("data",new ArrayList<>());
				}else if(i==2) {
					threeMap.put("name", name.get(2));
					threeMap.put("type", "bar");
					threeMap.put("data",new ArrayList<>());
				}else if(i==3) {
					fourMap.put("name", name.get(3));
					fourMap.put("type", "bar");
					fourMap.put("data",new ArrayList<>());
				}else if(i==4) {
					fiveMap.put("name", name.get(4));
					fiveMap.put("type", "bar");
					fiveMap.put("data",new ArrayList<>());
				}else if(i==5) {
					sixMap.put("name", name.get(5));
					sixMap.put("type", "bar");
					sixMap.put("data",new ArrayList<>());
				}else if(i==6) {
					sevenMap.put("name", name.get(6));
					sevenMap.put("type", "bar");
					sevenMap.put("data",new ArrayList<>());
				}else if(i==7) {
					eightMap.put("name", name.get(7));
					eightMap.put("type", "bar");
					eightMap.put("data",new ArrayList<>());
				}else if(i==8) {
					nineMap.put("name", name.get(8));
					nineMap.put("type", "bar");
					nineMap.put("data",new ArrayList<>());
				}else if(i==9) {
					tenMap.put("name", name.get(9));
					tenMap.put("type", "bar");
					tenMap.put("data",new ArrayList<>());
				}else if(i==10) {
					elevenMap.put("name", name.get(10));
					elevenMap.put("type", "bar");
					elevenMap.put("data",new ArrayList<>());
				}else if(i==11) {
					twelveMap.put("name", name.get(11));
					twelveMap.put("type", "bar");
					twelveMap.put("data",new ArrayList<>());
				}
			}
				
			for(int j = 0;j<one.size();j++) {
				if(j==0) {
					((List)oneMap.get("data")).add(one.get(0));
				}else if(j==1) {
					((List)twoMap.get("data")).add(one.get(1));
				}else if(j==2) {
					((List)threeMap.get("data")).add(one.get(2));
				}else if(j==3) {
					((List)fourMap.get("data")).add(one.get(3));
				}else if(j==4) {
					((List)fiveMap.get("data")).add(one.get(4));
				}else if(j==5) {
					((List)sixMap.get("data")).add(one.get(5));
				}else if(j==6) {
					((List)sevenMap.get("data")).add(one.get(6));
				}else if(j==7) {
					((List)eightMap.get("data")).add(one.get(7));
				}else if(j==8) {
					((List)nineMap.get("data")).add(one.get(8));
				}else if(j==9) {
					((List)tenMap.get("data")).add(one.get(9));
				}else if(j==10) {
					((List)elevenMap.get("data")).add(one.get(10));
				}else if(j==11) {
					((List)twelveMap.get("data")).add(one.get(11));
				}
			}	
				
			for(int j = 0;j<two.size();j++) {
				if(j==0) {
					((List)oneMap.get("data")).add(two.get(0));
				}else if(j==1) {
					((List)twoMap.get("data")).add(two.get(1));
				}else if(j==2) {
					((List)threeMap.get("data")).add(two.get(2));
				}else if(j==3) {
					((List)fourMap.get("data")).add(two.get(3));
				}else if(j==4) {
					((List)fiveMap.get("data")).add(two.get(4));
				}else if(j==5) {
					((List)sixMap.get("data")).add(two.get(5));
				}else if(j==6) {
					((List)sevenMap.get("data")).add(two.get(6));
				}else if(j==7) {
					((List)eightMap.get("data")).add(two.get(7));
				}else if(j==8) {
					((List)nineMap.get("data")).add(two.get(8));
				}else if(j==9) {
					((List)tenMap.get("data")).add(two.get(9));
				}else if(j==10) {
					((List)elevenMap.get("data")).add(two.get(10));
				}else if(j==11) {
					((List)twelveMap.get("data")).add(two.get(11));
				}
			}	
			for(int j = 0;j<three.size();j++) {
				if(j==0) {
					((List)oneMap.get("data")).add(three.get(0));
				}else if(j==1) {
					((List)twoMap.get("data")).add(three.get(1));
				}else if(j==2) {
					((List)threeMap.get("data")).add(three.get(2));
				}else if(j==3) {
					((List)fourMap.get("data")).add(three.get(3));
				}else if(j==4) {
					((List)fiveMap.get("data")).add(three.get(4));
				}else if(j==5) {
					((List)sixMap.get("data")).add(three.get(5));
				}else if(j==6) {
					((List)sevenMap.get("data")).add(three.get(6));
				}else if(j==7) {
					((List)eightMap.get("data")).add(three.get(7));
				}else if(j==8) {
					((List)nineMap.get("data")).add(three.get(8));
				}else if(j==9) {
					((List)tenMap.get("data")).add(three.get(9));
				}else if(j==10) {
					((List)elevenMap.get("data")).add(three.get(10));
				}else if(j==11) {
					((List)twelveMap.get("data")).add(three.get(11));
				}
			}	
			for(int j = 0;j<four.size();j++) {
				if(j==0) {
					((List)oneMap.get("data")).add(four.get(0));
				}else if(j==1) {
					((List)twoMap.get("data")).add(four.get(1));
				}else if(j==2) {
					((List)threeMap.get("data")).add(four.get(2));
				}else if(j==3) {
					((List)fourMap.get("data")).add(four.get(3));
				}else if(j==4) {
					((List)fiveMap.get("data")).add(four.get(4));
				}else if(j==5) {
					((List)sixMap.get("data")).add(four.get(5));
				}else if(j==6) {
					((List)sevenMap.get("data")).add(four.get(6));
				}else if(j==7) {
					((List)eightMap.get("data")).add(four.get(7));
				}else if(j==8) {
					((List)nineMap.get("data")).add(four.get(8));
				}else if(j==9) {
					((List)tenMap.get("data")).add(four.get(9));
				}else if(j==10) {
					((List)elevenMap.get("data")).add(four.get(10));
				}else if(j==11) {
					((List)twelveMap.get("data")).add(four.get(11));
				}
			}	
			for(int j = 0;j<five.size();j++) {
				if(j==0) {
					((List)oneMap.get("data")).add(five.get(0));
				}else if(j==1) {
					((List)twoMap.get("data")).add(five.get(1));
				}else if(j==2) {
					((List)threeMap.get("data")).add(five.get(2));
				}else if(j==3) {
					((List)fourMap.get("data")).add(five.get(3));
				}else if(j==4) {
					((List)fiveMap.get("data")).add(five.get(4));
				}else if(j==5) {
					((List)sixMap.get("data")).add(five.get(5));
				}else if(j==6) {
					((List)sevenMap.get("data")).add(five.get(6));
				}else if(j==7) {
					((List)eightMap.get("data")).add(five.get(7));
				}else if(j==8) {
					((List)nineMap.get("data")).add(five.get(8));
				}else if(j==9) {
					((List)tenMap.get("data")).add(five.get(9));
				}else if(j==10) {
					((List)elevenMap.get("data")).add(five.get(10));
				}else if(j==11) {
					((List)twelveMap.get("data")).add(five.get(11));
				}
			}
			for(int j = 0;j<six.size();j++) {
				if(j==0) {
					((List)oneMap.get("data")).add(six.get(0));
				}else if(j==1) {
					((List)twoMap.get("data")).add(six.get(1));
				}else if(j==2) {
					((List)threeMap.get("data")).add(six.get(2));
				}else if(j==3) {
					((List)fourMap.get("data")).add(six.get(3));
				}else if(j==4) {
					((List)fiveMap.get("data")).add(six.get(4));
				}else if(j==5) {
					((List)sixMap.get("data")).add(six.get(5));
				}else if(j==6) {
					((List)sevenMap.get("data")).add(six.get(6));
				}else if(j==7) {
					((List)eightMap.get("data")).add(six.get(7));
				}else if(j==8) {
					((List)nineMap.get("data")).add(six.get(8));
				}else if(j==9) {
					((List)tenMap.get("data")).add(six.get(9));
				}else if(j==10) {
					((List)elevenMap.get("data")).add(six.get(10));
				}else if(j==11) {
					((List)twelveMap.get("data")).add(six.get(11));
				}
			}
			for(int j = 0;j<seven.size();j++) {
				if(j==0) {
					((List)oneMap.get("data")).add(seven.get(0));
				}else if(j==1) {
					((List)twoMap.get("data")).add(seven.get(1));
				}else if(j==2) {
					((List)threeMap.get("data")).add(seven.get(2));
				}else if(j==3) {
					((List)fourMap.get("data")).add(seven.get(3));
				}else if(j==4) {
					((List)fiveMap.get("data")).add(seven.get(4));
				}else if(j==5) {
					((List)sixMap.get("data")).add(seven.get(5));
				}else if(j==6) {
					((List)sevenMap.get("data")).add(seven.get(6));
				}else if(j==7) {
					((List)eightMap.get("data")).add(seven.get(7));
				}else if(j==8) {
					((List)nineMap.get("data")).add(seven.get(8));
				}else if(j==9) {
					((List)tenMap.get("data")).add(seven.get(9));
				}else if(j==10) {
					((List)elevenMap.get("data")).add(seven.get(10));
				}else if(j==11) {
					((List)twelveMap.get("data")).add(seven.get(11));
				}
			}
			for(int j = 0;j<eight.size();j++) {
				if(j==0) {
					((List)oneMap.get("data")).add(eight.get(0));
				}else if(j==1) {
					((List)twoMap.get("data")).add(eight.get(1));
				}else if(j==2) {
					((List)threeMap.get("data")).add(eight.get(2));
				}else if(j==3) {
					((List)fourMap.get("data")).add(eight.get(3));
				}else if(j==4) {
					((List)fiveMap.get("data")).add(eight.get(4));
				}else if(j==5) {
					((List)sixMap.get("data")).add(eight.get(5));
				}else if(j==6) {
					((List)sevenMap.get("data")).add(eight.get(6));
				}else if(j==7) {
					((List)eightMap.get("data")).add(eight.get(7));
				}else if(j==8) {
					((List)nineMap.get("data")).add(eight.get(8));
				}else if(j==9) {
					((List)tenMap.get("data")).add(eight.get(9));
				}else if(j==10) {
					((List)elevenMap.get("data")).add(eight.get(10));
				}else if(j==11) {
					((List)twelveMap.get("data")).add(eight.get(11));
				}
			}
			for(int j = 0;j<nine.size();j++) {
				if(j==0) {
					((List)oneMap.get("data")).add(nine.get(0));
				}else if(j==1) {
					((List)twoMap.get("data")).add(nine.get(1));
				}else if(j==2) {
					((List)threeMap.get("data")).add(nine.get(2));
				}else if(j==3) {
					((List)fourMap.get("data")).add(nine.get(3));
				}else if(j==4) {
					((List)fiveMap.get("data")).add(nine.get(4));
				}else if(j==5) {
					((List)sixMap.get("data")).add(nine.get(5));
				}else if(j==6) {
					((List)sevenMap.get("data")).add(nine.get(6));
				}else if(j==7) {
					((List)eightMap.get("data")).add(nine.get(7));
				}else if(j==8) {
					((List)nineMap.get("data")).add(nine.get(8));
				}else if(j==9) {
					((List)tenMap.get("data")).add(nine.get(9));
				}else if(j==10) {
					((List)elevenMap.get("data")).add(nine.get(10));
				}else if(j==11) {
					((List)twelveMap.get("data")).add(nine.get(11));
				}
			}
			for(int j = 0;j<ten.size();j++) {
				if(j==0) {
					((List)oneMap.get("data")).add(ten.get(0));
				}else if(j==1) {
					((List)twoMap.get("data")).add(ten.get(1));
				}else if(j==2) {
					((List)threeMap.get("data")).add(ten.get(2));
				}else if(j==3) {
					((List)fourMap.get("data")).add(ten.get(3));
				}else if(j==4) {
					((List)fiveMap.get("data")).add(ten.get(4));
				}else if(j==5) {
					((List)sixMap.get("data")).add(ten.get(5));
				}else if(j==6) {
					((List)sevenMap.get("data")).add(ten.get(6));
				}else if(j==7) {
					((List)eightMap.get("data")).add(ten.get(7));
				}else if(j==8) {
					((List)nineMap.get("data")).add(ten.get(8));
				}else if(j==9) {
					((List)tenMap.get("data")).add(ten.get(9));
				}else if(j==10) {
					((List)elevenMap.get("data")).add(ten.get(10));
				}else if(j==11) {
					((List)twelveMap.get("data")).add(ten.get(11));
				}
			}
			for(int j = 0;j<eleven.size();j++) {
				if(j==0) {
					((List)oneMap.get("data")).add(eleven.get(0));
				}else if(j==1) {
					((List)twoMap.get("data")).add(eleven.get(1));
				}else if(j==2) {
					((List)threeMap.get("data")).add(eleven.get(2));
				}else if(j==3) {
					((List)fourMap.get("data")).add(eleven.get(3));
				}else if(j==4) {
					((List)fiveMap.get("data")).add(eleven.get(4));
				}else if(j==5) {
					((List)sixMap.get("data")).add(eleven.get(5));
				}else if(j==6) {
					((List)sevenMap.get("data")).add(eleven.get(6));
				}else if(j==7) {
					((List)eightMap.get("data")).add(eleven.get(7));
				}else if(j==8) {
					((List)nineMap.get("data")).add(eleven.get(8));
				}else if(j==9) {
					((List)tenMap.get("data")).add(eleven.get(9));
				}else if(j==10) {
					((List)elevenMap.get("data")).add(eleven.get(10));
				}else if(j==11) {
					((List)twelveMap.get("data")).add(eleven.get(11));
				}
			}	
			for(int j = 0;j<twelve.size();j++) {
				if(j==0) {
					((List)oneMap.get("data")).add(twelve.get(0));
				}else if(j==1) {
					((List)twoMap.get("data")).add(twelve.get(1));
				}else if(j==2) {
					((List)threeMap.get("data")).add(twelve.get(2));
				}else if(j==3) {
					((List)fourMap.get("data")).add(twelve.get(3));
				}else if(j==4) {
					((List)fiveMap.get("data")).add(twelve.get(4));
				}else if(j==5) {
					((List)sixMap.get("data")).add(twelve.get(5));
				}else if(j==6) {
					((List)sevenMap.get("data")).add(twelve.get(6));
				}else if(j==7) {
					((List)eightMap.get("data")).add(twelve.get(7));
				}else if(j==8) {
					((List)nineMap.get("data")).add(twelve.get(8));
				}else if(j==9) {
					((List)tenMap.get("data")).add(twelve.get(9));
				}else if(j==10) {
					((List)elevenMap.get("data")).add(twelve.get(10));
				}else if(j==11) {
					((List)twelveMap.get("data")).add(twelve.get(11));
				}
			}
			 List<Map<String,Object>> saList  = new ArrayList<>();
			 if(!oneMap.isEmpty()) {
				 saList.add(oneMap);
			 }
			 if(!twoMap.isEmpty()) {
				 saList.add(twoMap);
			 }
			 if(!threeMap.isEmpty()) {
				 saList.add(threeMap);
			 }
			 if(!fourMap.isEmpty()) {
				 saList.add(fourMap);
			 }
			 if(!fiveMap.isEmpty()) {
				 saList.add(fiveMap);
			 }
			 if(!sixMap.isEmpty()) {
				 saList.add(sixMap);
			 }
			 if(!sevenMap.isEmpty()) {
				 saList.add(sevenMap);
			 }
			 if(!eightMap.isEmpty()) {
				 saList.add(eightMap);
			 }
			 if(!nineMap.isEmpty()) {
				 saList.add(nineMap);
			 }
			 if(!tenMap.isEmpty()) {
				 saList.add(tenMap);
			 }
			 if(!elevenMap.isEmpty()) {
				 saList.add(elevenMap);
			 }
			 if(!twelveMap.isEmpty()) {
				 saList.add(twelveMap);
			 }
			 response.getWriter().println("["+new Gson().toJson(name)+","+new Gson().toJson(saList)+"]");
		
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 修改商品状态(上架/下架)
	 * @param request
	 * @param response
	 */
	private void editGoodsState(HttpServletRequest request, HttpServletResponse response) {
		int goods_id = 0;
		if(request.getParameter("goods_id")!=null&&!("".equals(request.getParameter("goods_id"))) ) {
			goods_id = Integer.valueOf(request.getParameter("goods_id"));
		}
		int state_id = 0;
		if(request.getParameter("state_id")!=null&&!("".equals(request.getParameter("state_id"))) ) {
			state_id = Integer.valueOf(request.getParameter("state_id"));
		}
		try {
			//调用业务层的方法修改商品的状态
			boolean b = false;
			if(state_id==3) {
				 b = backstageService.editGoodsState(goods_id, 4);
			}else {
				 b = backstageService.editGoodsState(goods_id, 3);
			}
			
			if(b) {
				response.getWriter().println(1);
			}else {
				response.getWriter().println(0);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 展示所有商品信息
	 * @param request
	 * @param response
	 */
	private void listGoodsInfo(HttpServletRequest request, HttpServletResponse response) {
		int pageNum = 1;
		int pageSize = 5;
		if(request.getParameter("pageNum")!=null&&!(request.getParameter("pageNum").equals(""))) {
			pageNum = Integer.valueOf(request.getParameter("pageNum"));
		}
		
		int gtype_id = 0;
		if(request.getParameter("gtype_id")!=null&&!(request.getParameter("gtype_id").equals(""))) {
			gtype_id = Integer.valueOf(request.getParameter("gtype_id"));
		}
		String goods = null;
		if(request.getParameter("goods")!=null&&!(request.getParameter("goods").equals(""))) {
			goods = request.getParameter("goods");
		}
		String goods_name = null;
		String goods_id = null;
		if(goods!=null) {
			if(goods.matches("[0-9]+")) {
				goods_id = goods;
			}else {
				goods_name = goods;
			}
		}
		try {
			//调用业务层的方法查询商品信息
			List<Goods> goodsInfoList = backstageService.selectGoodsInfo(goods_name, goods_id, pageNum, pageSize,gtype_id);
//			for(Goods goods2:goodsInfoList) {
//				System.out.println(goods);
//			}
			//调用业务层的方法查询商品数量
			long count = backstageService.selectGoodsCount(goods_id, goods_name,gtype_id);
			//调用业务层的方法查询所有的商品类型
			List<GoodsType> goodsTypeList = backstageService.selectGoodsType();
			long pageCount = (long)Math.ceil(count*1.0/pageSize);
			//计算一共几页
			//request绑定属性
			request.setAttribute("goodsInfoList", goodsInfoList);
			request.setAttribute("goods", goods);
			request.setAttribute("goodsTypeList", goodsTypeList);
			request.setAttribute("pageNum", pageNum);
			request.setAttribute("gtype_id",gtype_id);
			request.setAttribute("pageCount", pageCount);
			//将数据传递到goods_list.jsp页面
			request.getRequestDispatcher("/pages/houtai/goods/goods_list.jsp").forward(request, response);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

}
